package com.ferry.wallpaper.utils.cg;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

public class CodeUtils {

    private static final Random random = new Random();

    private static final char[] chars = {
            'a','b','c','d','e','f','g','h','i','j','k','l',
            'm','n','o','p','q','r','s','t','u','v','w','x','y','z',
            'A','B','C','D','E','F','G','H','I','J','K','L',
            'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
            '0','1','2','3','4','5','6','7','8','9','0'
    };

    public static Verification getCode(int width,int height,int textLength) throws IOException {
        BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        int oneWidth = (int) (width / (textLength * 1.0));
        Graphics2D graphics = bi.createGraphics();
        graphics.fillRect(0,0,bi.getWidth(),bi.getHeight());
        String randomText = getRandomText(textLength);
        for (int i = 0; i < randomText.length(); i++) {
            graphics.setColor(getRandomColor());
            graphics.setFont(getRandomFont(oneWidth));
            graphics.drawString(String.valueOf(randomText.charAt(i)),
                    i * oneWidth + (oneWidth/4),
                    oneWidth + (height/8));
        }
        graphics.setColor(getRandomColor());
        for (int i = 0; i < random.nextInt(300) + 100; i++) {
            graphics.fillRect(random.nextInt(width),random.nextInt(height),2,2);
        }
        return new Verification(ImageUtils.getBytes(bi,"jpg"),randomText);
    }


    /**
     * 获取随机文本
     * @param length
     * @return
     */
    public static String getRandomText(int length) {
        StringBuilder text = new StringBuilder();
        for (int i = 0; i < length; i++) {
            text.append(chars[random.nextInt(chars.length)]);
        }
        return text.toString();
    }

    /**
     * 获取随机颜色
     * @return
     */
    public static Color getRandomColor() {
        return new Color(random.nextInt(255),
                random.nextInt(255),
                random.nextInt(255));
    }

    private static final String[] fontName = new String[5];

    static {
        fontName[0] = FontUtils.ensureFontFamilyName("Arial");
        fontName[1] = FontUtils.ensureFontFamilyName("Georgia");
        fontName[2] = FontUtils.ensureFontFamilyName("Impact");
        fontName[3] = FontUtils.ensureFontFamilyName("Lucida Console");
        fontName[4] = FontUtils.ensureFontFamilyName("Console");
    }

    public static Font getRandomFont(int size) {
        return new Font(fontName[random.nextInt(5)],Font.PLAIN,size);
    }

    public static class Verification {
        private byte[] codeByes;
        private String text;
        public Verification(byte[] codeByes,String text) {
            this.codeByes = codeByes;
            this.text = text;
        }

        public byte[] getCodeByes() {
            return codeByes;
        }

        public String getText() {
            return text;
        }
    }
}